<?php
	include(ROOTPATH .'include'. DS .'protect.php');
	include ROOTPATH .'include'. DS .'isMyQuery.php';
	include(ROOTPATH .'include'. DS .'checkUserLogin.php');
	include(ROOTPATH .'class'. DS .'task.class.php');
	include_once(ROOTPATH .'class'. DS .'mediaFile.class.php');
	$task = new task ;
	
	
	$mediaFile = new mediaFile;
	
	$file_id			= getId('file_id');
	
	$fileName			= $_POST['fileName'];
	$tagMenu 			= $_POST['tagMenu'];
	$tagCustomMenu		= $_POST['tagCustomMenu'];
	$files 				= $_POST['file'];
	$fileTitles			= $_POST['fileTitle'];
	$fileDescriptions	= $_POST['fileDescription'];
	
	$mediaPassWord		= $_POST['mediaPassWord'];
	
	
	$mediaThumbPhoto	= $_POST['mediaThumbPhoto'] ;
	$mediaSource		= $_POST['mediaSource'];
	$mediaSourceTitles	= $_POST['mediaSourceTitles'];
	
	
	$description 		= $_POST['description'];
	$task_id			= getId('task');
	
	$user_id 			= $_SESSION['user_id'];	//$_POST['user_id'];
	
	//$file    .jpg | jpg        	moveto  UPLOADFILE_PATH
	//$mediaThumbPhoto; only one    moveto  MEDIAYHUMBPHOTO_PATH
	// $mediaSource;  |		moveto	MEDIAPHOTO_PATH
	//check
	
	$tag = new tag ;
	
	
	
	if( $files == '' )
		exit( '{"status":"error","msg":"你没有选择作品.请选择作品."}');
	if( $fileName == '' )
		exit( '{"status":"error","msg":"请填写作品名称."}');
	
	
	$files = explode('|',$files);
	
	//检测文件是否由用户自己上传
	if( $file_id )
		checkFileOwner($files, $field = 'file',$file_id,$user_id = $user -> id());
	
	// 移动	到指定目录
	$filesList = moveUploadFile($files, UPLOADFILE_PATH  );

	 
 
 	//如果不是默认图标
	
	
	if( $mediaThumbPhoto != MEDIA_THUMB && $mediaThumbPhoto != '' ){
		$root_path_count = strlen(ROOT_PATH);
		if( substr( $file ,0,$root_path_count) == ROOT_PATH ){
			$mediaThumbPhoto = substr( $mediaThumbPhoto , $root_path_count) ;
		}
		$mediaThumbPhoto = explode('|',$mediaThumbPhoto);		
		if( $file_id ){
			checkFileOwner($mediaThumbPhoto, $field = 'thumb',$file_id,$user_id = $user -> id());
		}
							
		$mediaThumbPhoto = moveUploadFile($mediaThumbPhoto, MEDIAYHUMBPHOTO_PATH );
	}else{
		//如果是默认图 ，就把把文件的第一个图片作为封面
		$thumbs = explode('|',$filesList);
		foreach($thumbs as $thumb){
			$fileType = getFileExt($thumb);
			if( $fileType == 'jpg' ||  $fileType == 'jpeg' ){
				
				$mediaThumbPhoto = explode('|',$thumb);
				$mediaThumbPhoto = moveUploadFile($mediaThumbPhoto, MEDIAYHUMBPHOTO_PATH ,'copy' , UPLOADFILE_PATH );	
				break ;
			}			
		}
		
		if( '' == $mediaThumbPhoto)
			;//$mediaThumbPhoto = MEDIA_THUMB ;
		else{
			$saveName = date('His') . rand(1000,9999) ;
			$savePath = MEDIAYHUMBPHOTO_PATH . creatFolder(MEDIAYHUMBPHOTO_PATH);
			$newPhoto = new userUploadPhoto ;
			$mediaThumbPhoto = $newPhoto -> cropUserPhoto($mediaThumbPhoto, $savePath , $saveName , MEDIA_THUMB_WIDTH , MEDIA_THUMB_HEIGHT   );
		}
	}
	if( '' != $mediaSource ){
		$mediaSource = explode('|',$mediaSource);
		if( $file_id )
			checkFileOwner($mediaSource, $field = 'mediaSource',$file_id,$user_id = $user -> id());
		$mediaSourceList = moveUploadFile($mediaSource, UPLOADFILE_PATH);	
	}
	
	
	
	
	$array['name'] 					= $fileName ;
	$array['file'] 					= $filesList ;
	$array['fileTitles'] 			= $fileTitles;
	$array['fileDescription'] 		= $fileDescriptions;
	
	$array['tag'] 					= $tagMenu ;
	$array['thumb']					= $mediaThumbPhoto;
	$array['mediaSource']			= $mediaSourceList;
	$array['mediaSourceTitles']		= $mediaSourceTitles;
	
	$array['description'] 			= $description;
	$array['passWord'] 				= $mediaPassWord;	
	$array['time'] 					= date('Y-m-d H:i:s',time());
	$array['ip'] 					= ip() ;
	
	if( !$file_id ){		
		$array['user_id'] 			= $user_id ;
		$newId = $dbs -> insert( DB_PRE.'file' ,$array , 'id' );
	}else{
		
		$lists = $dbs -> select('SELECT * FROM `'.DB_PRE.'file` WHERE `id` = '. $file_id .' and `user_id` = '. $user -> id() );
		$lists = $lists[0];
		if( (count($lists) == 0 || $lists['user_id'] != $user ->id() ) && $user -> powder() < 9)
			exit('{"status":"error","msg":"这不是您的作品,您无权修改."}');
		if( $lists['lock'] == '1' )
			exit('{"status":"error","msg":"此作品被锁定,不能修改."}');
		
		
		
		// delete old thumb //$mediaThumbPhoto
//		$thumbArray = explode( '/', $lists['thumb'] );
//		$thumbPath = str_replace( DS , '' , MEDIAPHOTO_PATH );
//		if( $thumbPath == $thumbArray[0] ){
//			$mediaFile -> delFile($lists['thumb']);
//		}
		
		// delete old thumb //$mediaThumbPhoto
		$fileArray = explode( '|', $lists['thumb'] );
		$fileDel = array_diff( $fileArray , explode( '|', $array['thumb'] ) );
		$fileDel = implode('|',$fileDel);
		if( count($fileDel) != 0 ){
			$mediaFile -> delFile( $fileDel );
		}
		
		//$array['file']
		$fileArray = explode( '|', $lists['file'] );
		$fileDel = array_diff( $fileArray , explode( '|', $array['file'] ) );
		$fileDel = implode('|',$fileDel);
		if( count($fileDel) != 0 ){
			$mediaFile -> delFile( $fileDel );
		}
		
		//$array['mediaSource']
		$fileArray = explode( '|', $lists['mediaSource'] );
		$fileDel = array_diff( $fileArray , explode( '|',  $array['mediaSource']) );
		$fileDel = implode('|',$fileDel);
		if( count($fileDel) != 0 ){
			$mediaFile -> delFile( $fileDel );
		}
		
		
		
		$dbs -> update( DB_PRE.'file' ,$array , ' `id` =  '. $file_id );
		$newId = $file_id ;
	}
	
	// task
	if( $task_id != 0  )
	$task -> taskAttend($task_id,$newId,true );
	
	$array = '';
	
	
	//tagCustomMenus parentId = 3;	
	
	$tagCustomMenuFormat = $tag -> saveCustomMenu($tagCustomMenu  );
	
	//exit('$tagCustomMenu='.$tagCustomMenu .',$tagCustomMenuFormat='.$tagCustomMenuFormat);
	//$tagMenu = 1!1,2,3,4|2!11,13|3!28,31|4!|5!|6!|7!54
	$tagMenus = $tagMenu . $tagCustomMenuFormat ;

	$tag -> saveTagMenu($tagMenus,$newId);
	$array['tag']	= $tagMenus;
	
	$dbs -> update( DB_PRE .'file' , $array , ' id = '. $newId );
	$newId = $encrypt -> code($newId);
	echo '{"status":"ok","url":"'. urlRewrite('option=media&id='.$newId) .'","msg":"'.lang_save_success.'"}';
?>